<template>
  <div class="modify-wrap">
    <el-dialog :visible.sync="dialogVisible_modify.show" center>
      <div class="show-title">任务标识ID:{{ task_id }}</div>
      <div class="center">
        <el-form :inline="true" :model="ruleform" ref="form" :rules="rules">
          <el-form-item label="任务名称" prop="task_name" label-width="120px" :label-position="right">
            <el-input v-model="ruleform.task_name" placeholder></el-input>
          </el-form-item>
          <br />
          <el-form-item label="循环间隔时间" prop="loop_interval" label-width="120px" :label-position="right">
            <el-input v-model="ruleform.loop_interval" placeholder></el-input>
          </el-form-item>
          <span class="second">秒</span>
          <br />
          <el-form-item class="btn-right">
            <el-button type="primary" size="small" @click="submitModify('form')"
              >修改</el-button
            >
            <el-button
              type="warning"
              size="small"
              plain
              @click="onReset('form')"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getData, postData } from "@/request/http";

export default {
  name: "audit",
  props: {
    task_id: {
      type: String,
      default: "",
    },
    task_name: {
      type: String,
      default: "",
    },
    loop_interval: {
      type: Number,
      default: "",
    },
    dialogVisible_modify: {
      type: Object,
      default() {
        return {
          show: false,
        };
      },
    },
  },
  data() {
    var checkNumber = (rule, value, callback) => {
      if (value != "") {
        if (/^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/.test(value) == false) {
          callback(new Error("请输入大于0的数字"));
        } else {
          callback();
        }
      } else {
        callback();
      }
      callback();
    };
    return {
      task_id_check: "",
      ruleform: {
        task_name: "",
        loop_interval: 60,
      },
      rules: {
        task_name: [{ required: false, message: "请输入", trigger: "blur" }],
        loop_interval: [
          { required: false, message: "请输入数字" },
          { validator: checkNumber, trigger: "blur" },
        ],
      },
    };
  },
  watch: {
    task_id: {
      handler(newVal) {
        this.task_id_check = newVal;
      },
      immediate: true,
    },
    // task_name: {
    //   handler(newVal) {
    //     this.task_name_check = newVal;
    //   },
    //   immediate: true
    // }
  },
  mounted() {
    this.ruleform.task_name = this.task_name;
    this.ruleform.loop_interval = this.loop_interval ? this.loop_interval : 60;
  },
  methods: {
    //修改 提交
    submitModify(formName) {
      var that = this;
      that.$refs[formName].validate((valid) => {
        if (
          that.ruleform.task_name == "" &&
          that.ruleform.loop_interval == ""
        ) {
          that.$message.error("必输项未输入，无法修改");
          return;
        }
        if (that.ruleform.task_name == "") {
          that.$message.error("修改失败，任务名称不能为空");
          return;
        }

        if (that.ruleform.loop_interval == "") {
          that.$message.error("循环时间不能为空");
          return;
        }

        // if (valid) {
        postData("/adminweb/video/live/playing/looptask/update", {
          task_id: that.task_id_check,
          task_name: that.ruleform.task_name,
          loop_interval: that.ruleform.loop_interval,
        })
          .then((res) => {
            if (res.code == 0) {
              that.$message.success("修改成功！");
              // that.dialogVisible_modify = { show: false };
              that.$emit("refreshTableList");
            } else {
              that.$message.error(res.code + "，" + res.msg);
            }
          })
          .catch((res) => {
            that.$message.error(res.code + "，" + res.msg);
          });
        // } else {
        //   that.$message.error("error");
        //   return false;
        // }
      });
    },
    // 重置
    onReset(val) {
      //this.$emit("reset", true);
      this.$refs[val].resetFields();
    },
  },
};
</script>

<style scoped>
.center {
  margin-top: 30px;
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}
.show-title {
  height: 30px;
  line-height: 30px;
  font-weight: 600;
  color: #676a6d;
}

.second {
  height: 40px;
  display: inline-block;
  line-height: 40px;
  font-size: 15px;
  color: #676a6d;
  margin-right: 30px;
}
.btn-right{
    float: right;
}
/* .modify-wrap >>> .el-form{
    width: 100%!important;
} */
.modify-wrap >>> .el-dialog--center .el-dialog__body {
  padding: 20px !important;
  height: 300px !important;
}
.modify-wrap >>> .el-dialog{
     width: 28%;
  overflow-y: scroll;
    margin-top:30vh!important;
}
</style>
